﻿using FlyingEye.Common.Utils;
using FlyingEye.Users;
using FlyingEye.Users.Models;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;

namespace FlyingEye
{
    /// <summary>
    /// 由 IDataSeeder 自动调用播种。
    /// </summary>
    public class MySqlDataSeederContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IUserRepository _userRepository;

        private readonly ApplicationConfiguration _appConfiguration;

        public MySqlDataSeederContributor(IUserRepository userRepository, ApplicationConfiguration applicationConfiguration)
        {
            _appConfiguration = applicationConfiguration;
            _userRepository = userRepository;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            var admin = await _userRepository.GetUserByNameAsync("admin");

            if (admin == null)
            {
                // 添加默认管理账号。
                await _userRepository.AddUserAsync(new UserModel("admin", _appConfiguration.ConvertToEncryptedBase64String("admin"), UserPermissions.Admin));
            }
        }
    }
}
